package com.hns.dev.linys.guava.math;

import com.google.common.math.IntMath;

import java.math.RoundingMode;

/**
 * Created by admin on 2019/2/16.
 */
public class GuavaIntMath {

    public static void main(String args[]){
        GuavaIntMath tester = new GuavaIntMath();
        tester.testIntMath();
    }

    private void testIntMath(){
        try{
            System.out.println(IntMath.checkedAdd(Integer.MAX_VALUE, Integer.MAX_VALUE));
        }catch(ArithmeticException e){
            System.out.println("Error: " + e.getMessage());
        }

        System.out.println(IntMath.divide(100, 5, RoundingMode.UNNECESSARY));
        try{
            //exception will be thrown as 100 is not completely divisible by 3 thus rounding
            // is required, and RoundingMode is set as UNNESSARY
            System.out.println(IntMath.divide(100, 3, RoundingMode.UNNECESSARY));
        }catch(ArithmeticException e){
            System.out.println("Error: " + e.getMessage());
        }

        System.out.println("Log2(2): "+IntMath.log2(2, RoundingMode.HALF_EVEN));

        System.out.println("Log10(10): "+IntMath.log10(10, RoundingMode.HALF_EVEN));

        System.out.println("sqrt(100): "+IntMath.sqrt(IntMath.pow(10,2), RoundingMode.HALF_EVEN));

        //最大公约数
        System.out.println("gcd(45,18): "+IntMath.gcd(45,18));

        //取摸
        System.out.println("modulus(100,3): "+IntMath.mod(100,3));

        //阶层
        System.out.println("factorial(5): "+IntMath.factorial(5));
    }



}
